package com;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RulesManager extends Database{

	public void defineRule(HttpServletRequest request, HttpServletResponse response){
		try{
			// See if the request was null
			if (request.getParameter("dataField") == null) return;
			
			connectToDatabase();
			
			preparedStatement = connect.prepareStatement("REPLACE INTO `citiquantsdb`.`rules`" + 
				"(`Rule Name`," + 
				"`Data Field`," +
				"`Specification`," +
				"`Boundary Type`," +
				"`Boundary Condition`)" + 
				"VALUES(?, ?, ?, ?, ?);");
			
			preparedStatement.setString(1, request.getParameter("ruleName"));
			preparedStatement.setString(2, request.getParameter("dataField"));
			preparedStatement.setString(3, request.getParameter("specification"));
			preparedStatement.setString(4, request.getParameter("boundaryType"));
			preparedStatement.setString(5, request.getParameter("boundaryCondition"));
			
			preparedStatement.executeUpdate();
			
			// Now check for new violations and add them to the database
			preparedStatement = connect.prepareStatement("SELECT * FROM `citiquantsdb`.`rules` WHERE `Rule Name` = ?;");
			preparedStatement.setString(1, request.getParameter("ruleName"));
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()){
				ViolationsManager violationsManager = new ViolationsManager();
				violationsManager.addViolations(resultSet.getInt("ID"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}
	
	public void deleteRule(HttpServletRequest request){
		try{
			connectToDatabase();
			
			// First delete all violations of this rule
			ViolationsManager violationsManager = new ViolationsManager();
			violationsManager.deleteViolations(Integer.parseInt(request.getParameter("id")));
			
			// Now delete the rule
			preparedStatement = connect.prepareStatement("DELETE FROM `citiquantsdb`.`rules` WHERE ID = ?;");
			
			preparedStatement.setInt(1, Integer.parseInt(request.getParameter("id")));
			preparedStatement.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}
	
	// Calling queryRules(-1) will return all rules in the database
	public ArrayList<Rule> queryRules(int ruleID){
		ArrayList<Rule> ret = new ArrayList<Rule>();
		boolean needToClose = true;
		
		try{
			needToClose = (connect == null) ? true : connect.isClosed();
			
			connectToDatabase();
			
			if (ruleID == -1)
				preparedStatement = connect.prepareStatement("SELECT * FROM `citiquantsdb`.`rules`;");
			else
				preparedStatement = connect.prepareStatement("SELECT * FROM `citiquantsdb`.`rules` WHERE `ID` = \"" + ruleID + "\";");

			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next()){
				Rule r = new Rule(resultSet.getInt("ID"),
						resultSet.getString("Rule Name"),
						resultSet.getString("Data Field"),
						resultSet.getString("Specification"),
						resultSet.getString("Boundary Type"),
						resultSet.getString("Boundary Condition"));
				ret.add(r);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (needToClose)
				close();
		}
		return ret;
	}
		
}
